//http://pydoing.blogspot.tw/2012/06/java-api-window.html


import java.awt.*;
import java.awt.event.*;
 
public class window implements ActionListener {
    Frame frame1, frame2;
    Button button1, button2, button3, button4;
     
    public static void main(String[] args) {
        new window();
    }
 
    public window() {
        frame1 = new Frame("AWTDemo1");
        frame1.addWindowListener(new AdapterDemo());
        frame1.setSize(320, 240);
        frame1.setLayout(new FlowLayout());


        frame2 = new Frame("AWTDemo2");
        frame2.addWindowListener(new AdapterDemo());
        frame2.setSize(320, 240);
        frame2.setLayout(new FlowLayout());
 
        button1 = new Button("toBack");
        button2 = new Button("toFront");
        button3 = new Button("toBack");
        button4 = new Button("toFront");
         
        button1.setActionCommand("b1");
        button1.addActionListener(this);

        button2.setActionCommand("b2");
        button2.addActionListener(this);

        button3.setActionCommand("b3");
        button3.addActionListener(this);

        button4.setActionCommand("b4");
        button4.addActionListener(this);
         
        frame1.add(button1);
        frame1.add(button2);

        frame2.add(button3);
        frame2.add(button4);
         
        frame1.setVisible(true);
        frame2.setVisible(true);


    }
     
    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();
		System.out.println(cmd);

        if (cmd == "b1") {
            frame1.toBack();
        	frame1.setVisible(false);
        	frame2.setVisible(true);
        }
        if (cmd == "b2") {
            frame1.toFront();
        	frame1.setVisible(false);
        	frame2.setVisible(true);
        }
        if (cmd == "b3") {
            frame2.toBack();
       	 	frame2.setVisible(false);
       		 frame1.setVisible(true);
        }
        if (cmd == "b4") {
            frame2.toFront();
       	 	frame2.setVisible(false);
       		 frame1.setVisible(true);
        }
System.out.println("frame1="+frame1.isShowing());
System.out.println("frame2="+frame2.isShowing());
    }
}
 
class AdapterDemo extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
}